/**
 * 
 */
package com.asset.salesforce.controller.pages;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.domain.Call;
import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CallService;
import com.asset.salesforce.service.ContactService;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.LeadService;

// TODO: Auto-generated Javadoc
/**
 * The Class CallController.
 *
 * @author Hisham
 */
@Controller
public class CallController {

	/** The employee service. */
	@Autowired
	private EmployeeService employeeService;
	
	/** The contact service. */
	@Autowired
	private ContactService contactService;
	
	/** The lead service. */
	@Autowired
	private LeadService leadService;
	
	/** The call service. */
	@Autowired
	private CallService callService;
	
	/** The calendar service. */
	@Autowired
	private CalendarService calendarService;
	
	/**
	 * Gets the new call page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @param request the request
	 * @param id the id
	 * @return the new call page
	 */
	@RequestMapping(value = "/newCall", method = RequestMethod.GET)
	public String getNewCallPage(HttpSession session, Model model,HttpServletResponse response,HttpServletRequest request,@RequestParam Integer id) {
		if (SecurityUtils.getSubject().hasRole("callAdd")){
			try {
				request.setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if(null ==session.getAttribute("userDetails")){
				return "redirect:login";
			}
			Employee employee = (Employee)session.getAttribute("userDetails");
			List<Employee>employees = employeeService.getAllCompanyEmployess(employee.getCompany().getCompanyName());
			model.addAttribute("employees",employees);
			model.addAttribute("parentId", id);
			List<Contact>contacts = contactService.getContactsByParentType(id, "lead");
			model.addAttribute("contacts",contacts);
			model.addAttribute("callDTO",new Call());
			return "createCallPage";
			}
			else{
				return "accessDenied";
			}
	}
	
	/**
	 * Post new call.
	 *
	 * @param model the model
	 * @param session the session
	 * @param request the request
	 * @param callForm the call form
	 * @param result the result
	 * @return the string
	 */
	@RequestMapping(value="/newCall",method=RequestMethod.POST)
	public String postNewCall(Model model,HttpSession session,HttpServletRequest request,@Valid @ModelAttribute("callDTO") Call callForm,BindingResult result) {		
		
		if(null ==session.getAttribute("userDetails") ){return "redirect:login";}
		
		Call call = new Call();
		if(callForm.getCrmStaff() != null){
			call.setCrmStaff(employeeService.getById(callForm.getCrmStaff().getId()));
		}
		if(callForm.getContactedPerson() != null){
			call.setContactedPerson(contactService.getById(callForm.getContactedPerson().getId()));
		}
		if(callForm.getDate() != null){
			call.setDate(callForm.getDate());
		}
		if(callForm.getStartTime()!=null){
			call.setStartTime(callForm.getStartTime());
		}
		if(callForm.getEndTime()!=null){
			call.setEndTime(callForm.getEndTime());
		}
		if(callForm.getInOutCall() != null){
			call.setInOutCall(callForm.getInOutCall());
		}
		if(callForm.getDecisionMaker() != null){
			call.setDecisionMaker(callForm.getDecisionMaker());
		}
		if(callForm.getComment() != null){
			call.setComment(callForm.getComment());
		}
		if(callForm.getNextFollowUp() != null){
			call.setNextFollowUp(callForm.getNextFollowUp());
		}
		if(callForm.getFollowUpType() != null&&!(callForm.getFollowUpType().equalsIgnoreCase(""))){
			call.setFollowUpType(callForm.getFollowUpType());
		}
		if(callForm.getFollowUpDate() != null){
			call.setFollowUpDate(callForm.getFollowUpDate());
		}
		if(callForm.getFollowUpDetails() != null&&!(callForm.getFollowUpDetails().equalsIgnoreCase(""))){
			call.setFollowUpDetails(callForm.getFollowUpDetails());
		}
		call.setParentId(callForm.getParentId());
		call.setParentType("lead");
		call.setDateCreated(new Date());
		callService.add(call);
		if(callForm.getFollowUpDate() != null){
			calendarService.callEvent(call);
		}
		return "redirect:leads";
	}
	
	/**
	 * Gets the edits the call page.
	 *
	 * @param session the session
	 * @param model the model
	 * @param response the response
	 * @param request the request
	 * @param id the id
	 * @param parentId the parent id
	 * @return the edits the call page
	 */
	@RequestMapping(value = "/editCall", method = RequestMethod.GET)
	public String getEditCallPage(HttpSession session, Model model,HttpServletResponse response,HttpServletRequest request,@RequestParam Integer id,@RequestParam Integer parentId) {
		if (SecurityUtils.getSubject().hasRole("callEdit")){
			try {
				request.setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if(null ==session.getAttribute("userDetails")){
				return "redirect:login";
			}
			Employee employee = (Employee)session.getAttribute("userDetails");
			List<Employee>employees = employeeService.getAllCompanyEmployess(employee.getCompany().getCompanyName());
			model.addAttribute("employees",employees);
			model.addAttribute("parentId", parentId);
			model.addAttribute("id", id);
			List<Contact>contacts = contactService.getContactsByParentType(parentId, "lead");
			model.addAttribute("contacts",contacts);
			Call call = callService.getById(id);
			model.addAttribute("callDTO",call);
			return "editCallPage";
			}
			else{
				return "accessDenied";
			}
	}
	
	/**
	 * Post edit call.
	 *
	 * @param model the model
	 * @param session the session
	 * @param request the request
	 * @param callForm the call form
	 * @param result the result
	 * @return the string
	 */
	@RequestMapping(value="/editCall",method=RequestMethod.POST)
	public String postEditCall(Model model,HttpSession session,HttpServletRequest request,@Valid @ModelAttribute("callDTO") Call callForm,BindingResult result) {		
		
		if(null ==session.getAttribute("userDetails") ){return "redirect:login";}
		
		Call call = callService.getById(callForm.getId());
		if(callForm.getCrmStaff() != null){
			call.setCrmStaff(employeeService.getById(callForm.getCrmStaff().getId()));
		}
		if(callForm.getContactedPerson() != null){
			call.setContactedPerson(contactService.getById(callForm.getContactedPerson().getId()));
		}
		if(callForm.getDate() != null){
			call.setDate(callForm.getDate());
		}
		if(callForm.getStartTime()!=null){
			call.setStartTime(callForm.getStartTime());
		}
		if(callForm.getEndTime()!=null){
			call.setEndTime(callForm.getEndTime());
		}
		if(callForm.getInOutCall() != null){
			call.setInOutCall(callForm.getInOutCall());
		}
		if(callForm.getDecisionMaker() != null){
			call.setDecisionMaker(callForm.getDecisionMaker());
		}
		if(callForm.getComment() != null){
			call.setComment(callForm.getComment());
		}
		if(callForm.getNextFollowUp() != null){
			call.setNextFollowUp(callForm.getNextFollowUp());
		}
		if(callForm.getFollowUpType() != null&&!(callForm.getFollowUpType().equalsIgnoreCase(""))){
			call.setFollowUpType(callForm.getFollowUpType());
		}
		if(callForm.getFollowUpDate() != null){
			call.setFollowUpDate(callForm.getFollowUpDate());
		}
		if(callForm.getFollowUpDetails() != null&&!(callForm.getFollowUpDetails().equalsIgnoreCase(""))){
			call.setFollowUpDetails(callForm.getFollowUpDetails());
		}
		call.setParentId(callForm.getParentId());
		call.setParentType("lead");
		call.setDateModified(new Date());
		callService.saveOrupdate(call);
		if(callForm.getFollowUpDate() != null){
			calendarService.callEvent(call);
		}
		return "redirect:leads";
	}
	
	/**
	 * View call.
	 *
	 * @param model the model
	 * @param session the session
	 * @param request the request
	 * @param id the id
	 * @return the string
	 */
	@RequestMapping(value="/viewCall",method=RequestMethod.GET)
	public String viewCall(Model model,HttpSession session,HttpServletRequest request,@RequestParam Integer id) {
		if (SecurityUtils.getSubject().hasRole("callView")){
		if(null ==session.getAttribute("userDetails") ){return "redirect:login";}
		
		Call call =callService.getById(id); 
		model.addAttribute("callDTO", call);
		return "viewCallPage";
		}else{
			return "accessDenied";
		}
	}
}
